package com.itown.expo.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.web.servlet.ModelAndView;

import webframe.common.dao.Pagination;
import webframe.common.helper.ControllerHelper;
import webframe.common.helper.FormatTransformHelper;

import com.itown.expo.controller.base.MyBaseCommandController;
import com.itown.expo.helper.MyControllerHelper;
import com.itown.expo.model.Goods;
import com.itown.expo.service.CheckBizService;

public class StatGoodsController extends MyBaseCommandController {
	private static Order[] orders = new Order[] { Order.desc("id") };

	@Override
	protected ModelAndView handleInner(HttpServletRequest request,
			HttpServletResponse response, Object command) {
		Goods cgoods = (Goods) command;
		DetachedCriteria criteria = MyControllerHelper
				.getGoodsDetachedCriteria(cgoods);
		applyCriteria(getServiceContainer().getCheckBizService(), criteria,
				cgoods);

		Map map = new HashMap();
		Pagination<Goods> page = ControllerHelper.queryPagination(request,
				orders, criteria);
		map.put("appPrefix", getAppPrefix());
		map.put("page", page);
		map.put("cgoods", cgoods);

		return new ModelAndView("StatGoods.jsp", "map", map);
	}

	// 应用查询条件
	public static void applyCriteria(CheckBizService checkBizService,
			DetachedCriteria criteria, Goods cgoods) {
		ControllerHelper.addEqualRestrictions(criteria, "id", cgoods.getId());
		ControllerHelper.addLikeRestrictions(criteria, "declNo", cgoods
				.getDeclNo());
		ControllerHelper.addEqualRestrictions(criteria, "backupCode", cgoods
				.getBackupCode());

		ControllerHelper.addORLikeRestrictions(criteria, new String[] {
				"goodsCname", "goodsEname" }, cgoods.getGoodsCname());
		ControllerHelper.addEqualRestrictions(criteria, "hsCode", cgoods
				.getHsCode());
		ControllerHelper.addLikeRestrictions(criteria, "hsCode", cgoods
				.getCiqCode());
		ControllerHelper.addLikeRestrictions(criteria, "inspMonitorCond",
				cgoods.getInspMonitorCond());
		ControllerHelper.addEqualRestrictions(criteria, "originCountryCode",
				cgoods.getOriginCountryCode());
		ControllerHelper.addEqualRestrictions(criteria, "inspectionResult",
				cgoods.getInspectionResult());
		ControllerHelper.addEqualRestrictions(criteria, "quarantineResult",
				cgoods.getQuarantineResult());

		if (cgoods.getExtraObject("beginDate") != null
				|| cgoods.getExtraObject("endDate") != null) {
			Date beginDate = null;
			Date endDate = null;
			beginDate = FormatTransformHelper.string2Date((String) cgoods
					.getExtraObject("beginDate"));
			endDate = FormatTransformHelper.string2Date((String) cgoods
					.getExtraObject("endDate"));

			List<String> list = checkBizService.findDeclNoByDate(beginDate,
					endDate);
			if (list.size() != 0) {
				ControllerHelper.addInRestrictions(criteria, "declNo", list
						.toArray(new String[0]));
			}
		}
	}
}